package im.frape;

import im.frape.core.FrapeCore;
import im.frape.model.FrapeModel;
import im.frape.model.JabberAccount;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class AccountList extends Activity {
	private static final String TAG = "AccountList";

	private FrapeCore core = FrapeCore.getInstance();
	private FrapeModel model = FrapeModel.getInstance();

	private ListView accountsListView;

	private static final int CONTEXT_CONNECT = 0;
	private static final int CONTEXT_DISCONNECT = 1;
	private static final int CONTEXT_EDIT = 2;
	private static final int CONTEXT_DELETE = 3;

	private static final int DIALOG_ADD = 0;

	// Dialog addAccountDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_list);
		initAccountsListView();
	}

	private void initAccountsListView() {
		accountsListView = (ListView) this.findViewById(R.id.accounts_list);
		registerForContextMenu(findViewById(R.id.accounts_list));
		refreshAccountsListView();
	}

	private void refreshAccountsListView() {
		accountsListView.setAdapter(new ArrayAdapter<JabberAccount>(this, R.layout.account_item, model.getAccounts()));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.account_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.add_account:
			showDialog(DIALOG_ADD);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;

		switch (id) {
		case DIALOG_ADD:
			AlertDialog.Builder builder;
			Context mContext = getApplicationContext();
			LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.account_add, (ViewGroup) findViewById(R.id.layout_root));

			builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.account_add);
			builder.setCancelable(true);
			builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					AlertDialog ad = (AlertDialog) dialog;
					EditText jabberId = (EditText) ad.findViewById(R.id.jabberId);
					EditText password = (EditText) ad.findViewById(R.id.password);
					core.addAccount(new JabberAccount(jabberId.getText().toString(), password.getText().toString()));
					refreshAccountsListView();
					Toast.makeText(getApplicationContext(), "Account added.", Toast.LENGTH_SHORT).show();
				}
			});
			builder.setNegativeButton(R.string.cancel, null);
			builder.setView(layout);
			dialog = builder.create();
		}
		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);

		switch (id) {
		case DIALOG_ADD:
			AlertDialog ad = (AlertDialog) dialog;
			EditText jabberId = (EditText) ad.findViewById(R.id.jabberId);
			EditText password = (EditText) ad.findViewById(R.id.password);
			jabberId.setText("");
			password.setText("");
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		menu.add(0, CONTEXT_CONNECT, 0, R.string.connect);
		menu.add(0, CONTEXT_DISCONNECT, 0, R.string.disconnect);
		menu.add(0, CONTEXT_EDIT, 0, R.string.edit);
		menu.add(0, CONTEXT_DELETE, 0, R.string.delete);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		JabberAccount account = model.getAccounts()[menuInfo.position];

		// TODO - functionality
		switch (item.getItemId()) {
		case CONTEXT_CONNECT:
			try {
				core.connectAccount(account);
				Toast.makeText(getApplicationContext(), "Connecting...", Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				Toast.makeText(getApplicationContext(), "Exception while trying to connect.", Toast.LENGTH_SHORT)
						.show();
			}
			return true;

		case CONTEXT_DISCONNECT:
			try {
				core.disconnectAccount(account);
				Toast.makeText(getApplicationContext(), "Disconnecting...", Toast.LENGTH_SHORT).show();
			} catch (Exception e) {
				Toast.makeText(getApplicationContext(), "Exception while trying to disconnect.", Toast.LENGTH_SHORT)
						.show();
			}
			return true;

		case CONTEXT_EDIT:
			try {
				Intent intent = new Intent(this, AccountEdit.class);
				startActivity(intent);
			} catch (Exception e) {
				Log.e(TAG, "Failed to launch AccountEdit Activity " + e.getMessage());
			}
			return true;

		case CONTEXT_DELETE:
			core.removeAccount(account);
			refreshAccountsListView();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
}
